//
//  ApplyViewController.m
//  OA
//
//  Created by c on 16/8/5.
//  Copyright © 2016年 王文晓. All rights reserved.
//

#import "ApplyViewController.h"
#import "BaseCollectionViewCell.h"
#import "ApplyTableViewCell.h"

@interface ApplyViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) BOOL isApply;   //报销按钮的判断
@property (nonatomic, assign) BOOL isSee;       //报销流程的判断
@property (nonatomic, assign) BOOL isSection;   //分区是否打开的判断
@property (nonatomic, assign) BOOL isRowOpen;   //分区下的单元格是否打开（以下依次类推）
@property (nonatomic, assign) BOOL isRowOpen1;
@property (nonatomic, assign) BOOL isRowOpen2;
@property (nonatomic, assign) BOOL isRowOpen3;
@property (nonatomic, assign) BOOL isRowOpen4;

@end

@implementation ApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"申请";
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.tableView];
    // Do any additional setup after loading the view.
}

#pragma mark - -------UICollectionView代理方法--------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BaseCollectionViewCell *baseCell = nil;
    baseCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PersonalNeedCell" forIndexPath:indexPath];
    baseCell.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
    return baseCell;
}

#pragma mark - -----------tableView的代理方法--------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 6;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return self.isRowOpen?1:0;
    }else if (section == 1){
        
        return self.isRowOpen1?1:0;
    }else if (section == 2){
        
        return self.isRowOpen2?2:0;
    }else if (section == 3){
        
        return self.isRowOpen3?2:0;
    }else if (section == 4){
        
        return self.isRowOpen4?2:0;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ApplyTableViewCell *applyCell = [tableView dequeueReusableCellWithIdentifier:@"ApplyCell" forIndexPath:indexPath];
    return applyCell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];
    NSArray *array = [NSArray arrayWithObjects:@"报销",@"请假",@"办公",@"培训",@"出差",@"用车", nil];
    
    for (int i = 0; i < section+1; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 40)];
        imageView.backgroundColor = [UIColor grayColor];
        [view addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 50, 40)];
        label.text = array[section];
        [view addSubview:label];
        
        UIButton *rowBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [rowBtn setFrame:CGRectMake(300, 10, 50, 40)];
        [rowBtn setTitle:@"⌃" forState:UIControlStateNormal];
        [rowBtn addTarget:self action:@selector(sectionBtn:) forControlEvents:UIControlEventTouchUpInside];
        rowBtn.tag = 3000+i;
        [view addSubview:rowBtn];
        
        UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 78, self.view.frame.size.width, 1)];
        lineLabel.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.2];
        [view addSubview:lineLabel];
    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 80;
}

//各个单元格箭头点击回调事件
- (void)sectionBtn:(UIButton *)sender {
    if (sender.tag == 3000) {
        
        self.isRowOpen = !self.isRowOpen;
        [sender setTitle:@"⌄" forState:UIControlStateNormal];
    }if (sender.tag == 3001) {
        
        self.isRowOpen1 = !self.isRowOpen1;
    }if (sender.tag == 3002) {
        
        self.isRowOpen2 = !self.isRowOpen2;
    }if (sender.tag == 3003) {
        
        self.isRowOpen3 = !self.isRowOpen3;
    }if (sender.tag == 3004) {
        
        self.isRowOpen4 = !self.isRowOpen4;
    }
    NSLog(@"btn %ld",sender.tag);
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - -------laying-------
- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 1, 1);
        flowLayout.itemSize = CGSizeMake((self.view.frame.size.width-2)/2, 100);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerNib:[UINib nibWithNibName:@"PersonalNeedCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PersonalNeedCell"];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.scrollEnabled = NO;
        
    }
    return _collectionView;
}
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"ApplyTableViewCell" bundle:nil] forCellReuseIdentifier:@"ApplyCell"];
//        _tableView.tableHeaderView = [self setTableHeaderView];

    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
